library(foreign)
options(max.print = 999999)
setwd("data replication")

data <- read.dta("CCES16_Common_OUTPUT_Jul2017_VV.dta")

data$republican <- ifelse(data$pid7 %in% c("Lean Republican", "Not very strong Republican", "Strong Republican"), 1, 0)
data$independent <- ifelse(data$pid7 == "Independent", 1, 0)

data$ideology <- as.numeric(data$ideo5)
table(data$ideology)
data$ideology <- ifelse(data$ideology == 6, 3, data$ideology)
data$ideology <- ifelse(data$ideology == 7, NA, data$ideology)

#republicans who voted in general and primary elections
gop_voted_voted <- ifelse(data$republican == 1 & data$CC16_328 != "Not Asked" 
                          & data$CC16_410a != "I didn't vote in this election"
                          & data$CC16_410a != "Not Asked", 1, 0)

#CC328 is primary election, #CC410a is general election
#people who never voted for Trump, including by not voting
data$never_trump <- 0
data$never_trump[data$republican == 1 & data$CC16_328 != "Donald Trump" & data$CC16_410a != "Donald Trump (Republican)"] <- 1

#people who never voted for Trump, but voted in primary and general
data$never_trump_voter <- 0
data$never_trump_voter[data$republican == 1 & data$CC16_328 != "Donald Trump" & data$CC16_328 != "Not Asked" & 
                         data$CC16_410a != "Donald Trump (Republican)" 
                       & data$CC16_410a != "Not Asked"] <- 1 

#people who voted for trump in general, but didn't in the primary, including by not voting in primary
data$gen_trump <- 0
data$gen_trump[data$republican == 1 & data$CC16_328 != "Donald Trump" & data$CC16_410a == "Donald Trump (Republican)"] <- 1

#people who voted for trump in general, but didn't in the primary, but did vote in primary
data$prim_no_trump_trump = 0
data$prim_no_trump_trump[data$republican == 1 & data$CC16_328 != "Donald Trump" & data$CC16_328 != "Not Asked" 
                         & data$CC16_410a == "Donald Trump (Republican)"] <- 1

#people who voted didn't vote at all in primary, but did for Trump in general 
data$no_prim_trump <- 0
data$no_prim_trump[data$republican == 1 & data$CC16_328 == "Not Asked" & data$CC16_410a == "Donald Trump (Republican)"] <- 1

#people who voted for trump in primary and general
data$trump_trump <- 0
data$trump_trump[data$republican == 1 & data$CC16_328 == "Donald Trump" & data$CC16_410a == "Donald Trump (Republican)"] <- 1


################################################################
yougov <- read.csv("BYU_YouGov_2017_Survey.csv")

yougov$republican <- ifelse(yougov$pid7 %in% c(5, 6, 7), 1, 0)

#republicans who voted in general and primary elections
gop_voted_voted_yg <- ifelse(yougov$republican == 1 & yougov$pp_repprim16 != 9
                          & yougov$presvote16post != 9
                          & yougov$presvote16post != 7, 1, 0)

#people who never voted for Trump, including by not voting
yougov$never_trump <- 0
yougov$never_trump[yougov$republican == 1 & yougov$pp_repprim16 != 1 & yougov$presvote16post != 2] <- 1

#people who never voted for Trump, but voted in primary and general
yougov$never_trump_voter_yg <- 0
yougov$never_trump_voter_yg[yougov$republican == 1 & yougov$pp_repprim16 != 1 & yougov$pp_primary16 == 2 
                       & yougov$presvote16post != 2 
                       & yougov$turnout16 == 1] <- 1 

#people who voted for trump in general, but didn't in the primary, including by not voting in primary
yougov$gen_trump <- 0
yougov$gen_trump[yougov$republican == 1 & yougov$pp_repprim16 != 1 & yougov$presvote16post == 2] <- 1

#people who voted for trump in general, but didn't in the primary, but did vote in primary
yougov$prim_no_trump_trump = 0
yougov$prim_no_trump_trump[yougov$republican == 1 & yougov$pp_repprim16 != 1 & yougov$pp_primary16 == 2 
                         & yougov$presvote16post == 2] <- 1

#people who voted for trump in primary and general
yougov$trump_trump <- 0
yougov$trump_trump[yougov$republican == 1 & yougov$pp_repprim16 == 1 & yougov$presvote16post == 2] <- 1



################################################################

#Figure 1a
#proportions of these three groups among Republicans
never_trump_voter_prop <- table(data$never_trump_voter)[2]/table(gop_voted_voted)[2]
general_trump_voter_prop <- table(data$prim_no_trump_trump)[2]/table(gop_voted_voted)[2]
trump_trump_voter_prop <- table(data$trump_trump)[2]/table(gop_voted_voted)[2]

plot(1:3, c(never_trump_voter_prop, general_trump_voter_prop, trump_trump_voter_prop), cex = 0, ylim = c(0, 1), xlim = c(.8, 3.2), 
     ylab = "Proportion", xlab = "", axes = F, 
     main = "2016 CCES")
segments(x0 = 1, x1 = 1, y0 = 0, y1 = never_trump_voter_prop, lend = 1, lwd = 30, col = "#ff0000")
segments(x0 = 2, x1 = 2, y0 = 0, y1 = general_trump_voter_prop, lend = 1, lwd = 30, col = "#cc5f5f")
segments(x0 = 3, x1 = 3, y0 = 0, y1 = trump_trump_voter_prop, lend = 1, lwd = 30, col = "#771d1d")
axis(1, at = 1:3, labels = c("Never Trump\n", "General Election\nOnly", "Primary & General\nElection"), cex.axis = .8, tick = T)
axis(2, at = seq(0, 1, .2), las = 2)


#Figure 1b
#proportion of people in these three groups
never_trump_voter_prop_yg <- table(yougov$never_trump_voter)[2]/table(gop_voted_voted_yg)[2]
general_trump_voter_prop_yg <- table(yougov$prim_no_trump_trump)[2]/table(gop_voted_voted_yg)[2]
trump_trump_voter_prop_yg <- table(yougov$trump_trump)[2]/table(gop_voted_voted_yg)[2]

#proportions of each of the three groups
plot(1:3, c(never_trump_voter_prop_yg, general_trump_voter_prop_yg, trump_trump_voter_prop_yg), cex = 0, ylim = c(0, 1), xlim = c(.8, 3.2), 
     ylab = "Proportion", xlab = "", axes = F, 
     main = "2017 YouGov")
segments(x0 = 1, x1 = 1, y0 = 0, y1 = never_trump_voter_prop_yg, lend = 1, lwd = 30, col = "#ff0000")
segments(x0 = 2, x1 = 2, y0 = 0, y1 = general_trump_voter_prop_yg, lend = 1, lwd = 30, col = "#cc5f5f")
segments(x0 = 3, x1 = 3, y0 = 0, y1 = trump_trump_voter_prop_yg, lend = 1, lwd = 30, col = "#771d1d")
axis(1, at = 1:3, labels = c("Never Trump\n", "General Election\nOnly", "Primary & General\nElection"), cex.axis = .8)
axis(2, at = seq(0, 1, .2), las = 2)

################################################################

#Figure 2a
#partisan leaning of these three groups
table(data$pid7[data$never_trump_voter == 1])
never_trump_voter_lean <- 437/(437+643+312)
never_trump_voter_weak <- 643/(437+643+312)
never_trump_voter_strong <- 312/(437+643+312)

table(data$pid7[data$prim_no_trump_trump == 1])
general_trump_voter_lean <- 1370/(1370+1303+2285)
general_trump_voter_weak <- 1303/(1370+1303+2285)
general_trump_voter_strong <- 2285/(1370+1303+2285)

table(data$pid7[data$trump_trump == 1])
trump_trump_voter_lean <- 1170/(1170+1536+2884)
trump_trump_voter_weak <- 1536/(1170+1536+2884)
trump_trump_voter_strong <- 2884/(1170+1536+2884)

plot(1:3, c(never_trump_voter_prop, general_trump_voter_prop, trump_trump_voter_prop)
     , cex = 0, ylim = c(0, 1), xlim = c(.8, 3.2), 
     ylab = "Proportion", xlab = "", axes = F, 
     main = "2016 CCES")
segments(x0 = c(.8,1.8,2.8), x1 = c(.8,1.8,2.8), y0 = c(0,0,0), 
         y1 = c(never_trump_voter_lean, never_trump_voter_weak, never_trump_voter_strong)
         , lend = 1, lwd = 10, col = "#ff0000")
segments(x0 = c(1,2,3), x1 = c(1,2,3), y0 = c(0,0,0), 
         y1 = c(general_trump_voter_lean, general_trump_voter_weak, general_trump_voter_strong)
         , lend = 1, lwd = 10, col = "#cc5f5f")
segments(x0 = c(1.2,2.2,3.2), x1 = c(1.2,2.2,3.2), y0 = c(0,0,0), 
         y1 = c(trump_trump_voter_lean, trump_trump_voter_weak, trump_trump_voter_strong)
         , lend = 1, lwd = 10, col = "#771d1d")
axis(1, at = 1:3, labels = c("Lean GOP", "Weak GOP", "Strong GOP"), cex.axis = .8)
axis(2, at = seq(0, 1, .2), las = 2)
text(.8, .7, "Never Trump", pos = 4, cex = .7)
text( 1, .63, "General Election Only", pos = 4, cex = .7)
text(1.2, .56, "Primary and General Election", pos = 4, cex = .7)
segments(x0 = .8, x1 = .9, y0 = .36, y1 = .67, lwd = 2)
segments(x0 = 1, x1 = 1.15, y0 = .31, y1 = .6, lwd = 2)
segments(x0 = 1.2, x1 = 1.4, y0 = .25, y1 = .52, lwd = 2)


#Figure 2b
#partisan leaning of these three groups
table(yougov$pid7[yougov$never_trump_voter == 1])
never_trump_voter_lean_yg <- 16/(16+17+14)
never_trump_voter_weak_yg <- 17/(16+17+14)
never_trump_voter_strong_yg <- 14/(16+17+14)

table(yougov$pid7[yougov$prim_no_trump_trump == 1])
general_trump_voter_lean_yg <- 49/(49+45+95)
general_trump_voter_weak_yg <- 45/(49+45+95)
general_trump_voter_strong_yg <- 95/(49+45+95)

table(yougov$pid7[yougov$trump_trump == 1])
trump_trump_voter_lean_yg <- 42/(42+69+128)
trump_trump_voter_weak_yg <- 69/(42+69+128)
trump_trump_voter_strong_yg <- 128/(42+69+128)

plot(1:3, c(never_trump_voter_prop_yg, general_trump_voter_prop_yg, trump_trump_voter_prop_yg)
     , cex = 0, ylim = c(0, 1), xlim = c(.8, 3.2), 
     ylab = "Proportion", xlab = "", axes = F, 
     main = "2017 YouGov")
segments(x0 = c(.8,1.8,2.8), x1 = c(.8,1.8,2.8), y0 = c(0,0,0), 
         y1 = c(never_trump_voter_lean_yg, never_trump_voter_weak_yg, never_trump_voter_strong_yg)
         , lend = 1, lwd = 10, col = "#ff0000")
segments(x0 = c(1,2,3), x1 = c(1,2,3), y0 = c(0,0,0), 
         y1 = c(general_trump_voter_lean_yg, general_trump_voter_weak_yg, general_trump_voter_strong_yg)
         , lend = 1, lwd = 10, col = "#cc5f5f")
segments(x0 = c(1.2,2.2,3.2), x1 = c(1.2,2.2,3.2), y0 = c(0,0,0), 
         y1 = c(trump_trump_voter_lean_yg, trump_trump_voter_weak_yg, trump_trump_voter_strong_yg)
         , lend = 1, lwd = 10, col = "#771d1d")
axis(1, at = 1:3, labels = c("Lean GOP", "Weak GOP", "Strong GOP"), cex.axis = .8)
axis(2, at = seq(0, 1, .2), las = 2)
text(.8, .7, "Never Trump", pos = 4, cex = .7)
text( 1, .6, "General Election Only", pos = 4, cex = .7)
text(1.2, .5, "Primary and General Election", pos = 4, cex = .7)
segments(x0 = .8, x1 = .9, y0 = .37, y1 = .67, lwd = 2)
segments(x0 = 1, x1 = 1.2, y0 = .3, y1 = .57, lwd = 2)
segments(x0 = 1.2, x1 = 1.4, y0 = .2, y1 = .46, lwd = 2)

################################################################
 
#Table 2
data$male <- ifelse(data$gender == "Male", 1, 0)
data$white <- ifelse(data$race == "White", 1, 0)
data$married <- ifelse(data$marstat %in% c("Married"), 1, 0)
data$full.time <- ifelse(data$employ == "Full-time", 1, 0)
data$retire <- ifelse(data$employ == "Retired", 1, 0)
data$income <- as.numeric(data$faminc)
data$income[data$income %in% c(18, 19)] <- NA
data$income[data$income == 17] <- 12
data$age <- 2017-data$birthyr
data$education <- as.numeric(data$educ)
data$unemploy <- ifelse(data$employ %in% c("Temporarily laid off", "Unemployed"), 1, 0)

#demographics of these three groups
summary(data$male[data$never_trump_voter == 1])
summary(data$male[data$prim_no_trump_trump == 1])
summary(data$male[data$trump_trump == 1])

summary(data$white[data$never_trump_voter == 1])
summary(data$white[data$prim_no_trump_trump == 1])
summary(data$white[data$trump_trump == 1])

summary(data$married[data$never_trump_voter == 1])
summary(data$married[data$prim_no_trump_trump == 1])
summary(data$married[data$trump_trump == 1])

summary(data$full.time[data$never_trump_voter == 1])
summary(data$full.time[data$prim_no_trump_trump == 1])
summary(data$full.time[data$trump_trump == 1])

summary(data$retire[data$never_trump_voter == 1])
summary(data$retire[data$prim_no_trump_trump == 1])
summary(data$retire[data$trump_trump == 1])

summary(data$income[data$never_trump_voter == 1])
summary(data$income[data$prim_no_trump_trump == 1])
summary(data$income[data$trump_trump == 1])

summary(data$age[data$never_trump_voter == 1])
summary(data$age[data$prim_no_trump_trump == 1])
summary(data$age[data$trump_trump == 1])

summary(data$education[data$never_trump_voter == 1])
summary(data$education[data$prim_no_trump_trump == 1])
summary(data$education[data$trump_trump == 1])

################################################################

#Figure 3 - self-identified ideology
ideology.all <- table(data$ideology[data$ideology < 6 & data$republican == 1])
ideology.all <- ideology.all / length(data$ideology[data$ideology < 6 & data$republican == 1])
  
ideology.never.trump.voters <- table(data$ideology[data$republican == 1 & data$ideology < 6 
                                                   & data$never_trump_voter == 1])
ideology.never.trump.voters <- ideology.never.trump.voters / length(data$ideology
                                                            [data$republican == 1 & data$ideology < 6 
                                                              & data$never_trump_voter == 1])

ideology.always.trump.voters <- table(data$ideology[data$republican == 1 & data$ideology < 6 
                                                   & data$trump_trump == 1])
ideology.always.trump.voters <- ideology.always.trump.voters / length(data$ideology
                                                                    [data$republican == 1 & data$ideology < 6 
                                                                      & data$trump_trump == 1])

ideology.general.trump.voters <- table(data$ideology[data$republican == 1 & data$ideology < 6 
                                                    & data$prim_no_trump_trump == 1])
ideology.general.trump.voters <- ideology.general.trump.voters / length(data$ideology
                                                                      [data$republican == 1 & data$ideology < 6 
                                                                        & data$prim_no_trump_trump == 1])

plot(1:5, ideology.never.trump.voters, cex = 0, ylim = c(0, .6), xlim = c(.8, 5.2), 
     ylab = "Proportion", xlab = "Self-Identified Ideology", axes = F, 
     main = "")
segments(x0 = .8:4.8, x1 = .8:4.8, y0 = 0, y1 = ideology.never.trump.voters, lend = 1, lwd = 10, col = "#ff0000")
segments(x0 = 1:5, x1 = 1:5, y0 = 0, y1 = ideology.general.trump.voters, lend = 1, lwd = 10, col = "#cc5f5f")
segments(x0 = 1.2:5.2, x1 = 1.2:5.2, y0 = 0, y1 = ideology.always.trump.voters, lend = 1, lwd = 10, col = "#771d1d")
axis(1, at = 1:5, labels = c("Very\nLiberal", "Liberal", "Moderate", "Conservative", "Very\nConservative"), cex.axis = .8)
axis(2, at = seq(0, .6, .1), las = 2)
text(3.8, .4, "Never Trump", cex = .7, pos = 2)
text(4, .55, "General Election Only", cex = .7, pos = 2)
text(4.2, .55, "Primary & General\nElection", cex = .7, pos = 4)


################################################################

#Figure 4 - operational ideology
data$gun1 <- NA
data$gun1[data$CC16_330a == "Support"] <- 1
data$gun1[data$CC16_330a == "Oppose"] <- 0
table(data$gun1)
a1 <- table(data$gun1[data$republican == 1 & data$never_trump_voter == 1])/length(data$gun1[data$republican == 1 & data$never_trump_voter == 1])
b1 <- table(data$gun1[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$gun1[data$republican == 1 & data$prim_no_trump_trump == 1])
c1 <- table(data$gun1[data$republican == 1 & data$trump_trump == 1])/length(data$gun1[data$republican == 1 & data$trump_trump == 1])
data$skipped1 <- ifelse(data$CC16_330a == "Skipped", 1, 0)

data$gun2 <- NA
data$gun2[data$CC16_330b == "Oppose"] <- 1
data$gun2[data$CC16_330b == "Support"] <- 0
table(data$gun2)
a2 <- table(data$gun2[data$republican == 1 & data$never_trump_voter == 1])/length(data$gun2[data$republican == 1 & data$never_trump_voter == 1])
b2 <- table(data$gun2[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$gun2[data$republican == 1 & data$prim_no_trump_trump == 1])
c2 <- table(data$gun2[data$republican == 1 & data$trump_trump == 1])/length(data$gun2[data$republican == 1 & data$trump_trump == 1])
data$skipped2 <- ifelse(data$CC16_330b == "Skipped", 1, 0)

data$gun3 <- NA
data$gun3[data$CC16_330d == "Support"] <- 1
data$gun3[data$CC16_330d == "Oppose"] <- 0
table(data$gun3)
a3 <- table(data$gun3[data$republican == 1 & data$never_trump_voter == 1])/length(data$gun3[data$republican == 1 & data$never_trump_voter == 1])
b3 <- table(data$gun3[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$gun3[data$republican == 1 & data$prim_no_trump_trump == 1])
c3 <- table(data$gun3[data$republican == 1 & data$trump_trump == 1])/length(data$gun3[data$republican == 1 & data$trump_trump == 1])
data$skipped3 <- ifelse(data$CC16_330d == "Skipped", 1, 0)

data$gun4 <- NA
data$gun4[data$CC16_330e == "Oppose"] <- 1
data$gun4[data$CC16_330e == "Support"] <- 0
table(data$gun4)
a4 <- table(data$gun4[data$republican == 1 & data$never_trump_voter == 1])/length(data$gun4[data$republican == 1 & data$never_trump_voter == 1])
b4 <- table(data$gun4[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$gun4[data$republican == 1 & data$prim_no_trump_trump == 1])
c4 <- table(data$gun4[data$republican == 1 & data$trump_trump == 1])/length(data$gun4[data$republican == 1 & data$trump_trump == 1])
data$skipped4 <- ifelse(data$CC16_330e == "Skipped", 1, 0)

data$immig1 <- NA
data$immig1[data$CC16_331_1 == "Yes"] <- 1
data$immig1[data$CC16_331_1 == "No"] <- 0
table(data$immig1)
a5 <- table(data$immig1[data$republican == 1 & data$never_trump_voter == 1])/length(data$immig1[data$republican == 1 & data$never_trump_voter == 1])
b5 <- table(data$immig1[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$immig1[data$republican == 1 & data$prim_no_trump_trump == 1])
c5 <- table(data$immig1[data$republican == 1 & data$trump_trump == 1])/length(data$immig1[data$republican == 1 & data$trump_trump == 1])

data$immig2 <- NA
data$immig2[data$CC16_331_2 == "No"] <- 1
data$immig2[data$CC16_331_2 == "Yes"] <- 0
table(data$immig2)
a6 <- table(data$immig2[data$republican == 1 & data$never_trump_voter == 1])/length(data$immig2[data$republican == 1 & data$never_trump_voter == 1])
b6 <- table(data$immig2[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$immig2[data$republican == 1 & data$prim_no_trump_trump == 1])
c6 <- table(data$immig2[data$republican == 1 & data$trump_trump == 1])/length(data$immig2[data$republican == 1 & data$trump_trump == 1])

data$immig3 <- NA
data$immig3[data$CC16_331_3 == "Yes"] <- 1
data$immig3[data$CC16_331_3 == "No"] <- 0
table(data$immig3)
a7 <- table(data$immig3[data$republican == 1 & data$never_trump_voter == 1])/length(data$immig3[data$republican == 1 & data$never_trump_voter == 1])
b7 <- table(data$immig3[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$immig3[data$republican == 1 & data$prim_no_trump_trump == 1])
c7 <- table(data$immig3[data$republican == 1 & data$trump_trump == 1])/length(data$immig3[data$republican == 1 & data$trump_trump == 1])

data$immig4 <- NA
data$immig4[data$CC16_331_7 == "No"] <- 1
data$immig4[data$CC16_331_7 == "Yes"] <- 0
table(data$immig4)
a8 <- table(data$immig4[data$republican == 1 & data$never_trump_voter == 1])/length(data$immig4[data$republican == 1 & data$never_trump_voter == 1])
b8 <- table(data$immig4[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$immig4[data$republican == 1 & data$prim_no_trump_trump == 1])
c8 <- table(data$immig4[data$republican == 1 & data$trump_trump == 1])/length(data$immig4[data$republican == 1 & data$trump_trump == 1])

data$abort1 <- NA
data$abort1[data$CC16_332a == "Support"] <- 1
data$abort1[data$CC16_332a == "Oppose"] <- 0
table(data$abort1)
a9 <- table(data$abort1[data$republican == 1 & data$never_trump_voter == 1])/length(data$abort1[data$republican == 1 & data$never_trump_voter == 1])
b9 <- table(data$abort1[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$abort1[data$republican == 1 & data$prim_no_trump_trump == 1])
c9 <- table(data$abort1[data$republican == 1 & data$trump_trump == 1])/length(data$abort1[data$republican == 1 & data$trump_trump == 1])
data$skipped9 <- ifelse(data$CC16_332a == "Skipped", 1, 0)

data$abort2 <- NA
data$abort2[data$CC16_332c == "Oppose"] <- 1
data$abort2[data$CC16_332c == "Support"] <- 0
table(data$abort2)
a10 <- table(data$abort2[data$republican == 1 & data$never_trump_voter == 1])/length(data$abort2[data$republican == 1 & data$never_trump_voter == 1])
b10 <- table(data$abort2[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$abort2[data$republican == 1 & data$prim_no_trump_trump == 1])
c10 <- table(data$abort2[data$republican == 1 & data$trump_trump == 1])/length(data$abort2[data$republican == 1 & data$trump_trump == 1])
data$skipped10 <- ifelse(data$CC16_332a == "Skipped", 1, 0)

data$abort3 <- NA
data$abort3[data$CC16_332d == "Oppose"] <- 1
data$abort3[data$CC16_332d == "Support"] <- 0
table(data$abort3)
a11 <- table(data$abort3[data$republican == 1 & data$never_trump_voter == 1])/length(data$abort3[data$republican == 1 & data$never_trump_voter == 1])
b11 <- table(data$abort3[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$abort3[data$republican == 1 & data$prim_no_trump_trump == 1])
c11 <- table(data$abort3[data$republican == 1 & data$trump_trump == 1])/length(data$abort3[data$republican == 1 & data$trump_trump == 1])
data$skipped11 <- ifelse(data$CC16_332d == "Skipped", 1, 0)

data$abort4 <- NA
data$abort4[data$CC16_332e == "Oppose"] <- 1
data$abort4[data$CC16_332e == "Support"] <- 0
table(data$abort4)
a12 <- table(data$abort4[data$republican == 1 & data$never_trump_voter == 1])/length(data$abort4[data$republican == 1 & data$never_trump_voter == 1])
b12 <- table(data$abort4[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$abort4[data$republican == 1 & data$prim_no_trump_trump == 1])
c12 <- table(data$abort4[data$republican == 1 & data$trump_trump == 1])/length(data$abort4[data$republican == 1 & data$trump_trump == 1])
data$skipped12 <- ifelse(data$CC16_332e == "Skipped", 1, 0)

data$abort5 <- NA
data$abort5[data$CC16_332f == "Oppose"] <- 1
data$abort5[data$CC16_332f == "Support"] <- 0
table(data$abort5)
a13 <- table(data$abort5[data$republican == 1 & data$never_trump_voter == 1])/length(data$abort5[data$republican == 1 & data$never_trump_voter == 1])
b13 <- table(data$abort5[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$abort5[data$republican == 1 & data$prim_no_trump_trump == 1])
c13 <- table(data$abort5[data$republican == 1 & data$trump_trump == 1])/length(data$abort5[data$republican == 1 & data$trump_trump == 1])
data$skipped13 <- ifelse(data$CC16_332f == "Skipped", 1, 0)

data$enviro1 <- NA
data$enviro1[data$CC16_333a == "Support"] <- 1
data$enviro1[data$CC16_333a == "Oppose"] <- 0
table(data$enviro1)
a14 <- table(data$enviro1[data$republican == 1 & data$never_trump_voter == 1])/length(data$enviro1[data$republican == 1 & data$never_trump_voter == 1])
b14 <- table(data$enviro1[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$enviro1[data$republican == 1 & data$prim_no_trump_trump == 1])
c14 <- table(data$enviro1[data$republican == 1 & data$trump_trump == 1])/length(data$enviro1[data$republican == 1 & data$trump_trump == 1])
data$skipped14 <- ifelse(data$CC16_333a == "Skipped", 1, 0)

data$enviro2 <- NA
data$enviro2[data$CC16_333b == "Support"] <- 1
data$enviro2[data$CC16_333b == "Oppose"] <- 0
table(data$enviro2)
a15 <- table(data$enviro2[data$republican == 1 & data$never_trump_voter == 1])/length(data$enviro2[data$republican == 1 & data$never_trump_voter == 1])
b15 <- table(data$enviro2[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$enviro2[data$republican == 1 & data$prim_no_trump_trump == 1])
c15 <- table(data$enviro2[data$republican == 1 & data$trump_trump == 1])/length(data$enviro2[data$republican == 1 & data$trump_trump == 1])
data$skipped15 <- ifelse(data$CC16_333b == "Skipped", 1, 0)

data$enviro3 <- NA
data$enviro3[data$CC16_333c == "Support"] <- 1
data$enviro3[data$CC16_333c == "Oppose"] <- 0
table(data$enviro3)
a16 <- table(data$enviro3[data$republican == 1 & data$never_trump_voter == 1])/length(data$enviro3[data$republican == 1 & data$never_trump_voter == 1])
b16 <- table(data$enviro3[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$enviro3[data$republican == 1 & data$prim_no_trump_trump == 1])
c16 <- table(data$enviro3[data$republican == 1 & data$trump_trump == 1])/length(data$enviro3[data$republican == 1 & data$trump_trump == 1])
data$skipped16 <- ifelse(data$CC16_333c == "Skipped", 1, 0)

data$enviro4 <- NA
data$enviro4[data$CC16_333d == "Support"] <- 1
data$enviro4[data$CC16_333d == "Oppose"] <- 0
table(data$enviro4)
a17 <- table(data$enviro4[data$republican == 1 & data$never_trump_voter == 1])/length(data$enviro4[data$republican == 1 & data$never_trump_voter == 1])
b17 <- table(data$enviro4[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$enviro4[data$republican == 1 & data$prim_no_trump_trump == 1])
c17 <- table(data$enviro4[data$republican == 1 & data$trump_trump == 1])/length(data$enviro4[data$republican == 1 & data$trump_trump == 1])
data$skipped17 <- ifelse(data$CC16_333d == "Skipped", 1, 0)

data$crime1 <- NA
data$crime1[data$CC16_334a == "Support"] <- 1
data$crime1[data$CC16_334a == "Oppose"] <- 0
table(data$crime1)
a18 <- table(data$crime1[data$republican == 1 & data$never_trump_voter == 1])/length(data$crime1[data$republican == 1 & data$never_trump_voter == 1])
b18 <- table(data$crime1[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$crime1[data$republican == 1 & data$prim_no_trump_trump == 1])
c18 <- table(data$crime1[data$republican == 1 & data$trump_trump == 1])/length(data$crime1[data$republican == 1 & data$trump_trump == 1])
data$skipped18 <- ifelse(data$CC16_334a == "Skipped", 1, 0)

data$crime2 <- NA
data$crime2[data$CC16_334b == "Support"] <- 1
data$crime2[data$CC16_334b == "Oppose"] <- 0
table(data$crime2)
a19 <- table(data$crime2[data$republican == 1 & data$never_trump_voter == 1])/length(data$crime2[data$republican == 1 & data$never_trump_voter == 1])
b19 <- table(data$crime2[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$crime2[data$republican == 1 & data$prim_no_trump_trump == 1])
c19 <- table(data$crime2[data$republican == 1 & data$trump_trump == 1])/length(data$crime2[data$republican == 1 & data$trump_trump == 1])
data$skipped19 <- ifelse(data$CC16_334b == "Skipped", 1, 0)

data$crime3 <- NA
data$crime3[data$CC16_334c == "Oppose"] <- 1
data$crime3[data$CC16_334c == "Support"] <- 0
table(data$crime3)
a20 <- table(data$crime3[data$republican == 1 & data$never_trump_voter == 1])/length(data$crime3[data$republican == 1 & data$never_trump_voter == 1])
b20 <- table(data$crime3[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$crime3[data$republican == 1 & data$prim_no_trump_trump == 1])
c20 <- table(data$crime3[data$republican == 1 & data$trump_trump == 1])/length(data$crime3[data$republican == 1 & data$trump_trump == 1])
data$skipped20 <- ifelse(data$CC16_334c == "Skipped", 1, 0)

data$crime4 <- NA
data$crime4[data$CC16_334d == "Oppose"] <- 1
data$crime4[data$CC16_334d == "Support"] <- 0
table(data$crime4)
a21 <- table(data$crime4[data$republican == 1 & data$never_trump_voter == 1])/length(data$crime4[data$republican == 1 & data$never_trump_voter == 1])
b21 <- table(data$crime4[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$crime4[data$republican == 1 & data$prim_no_trump_trump == 1])
c21 <- table(data$crime4[data$republican == 1 & data$trump_trump == 1])/length(data$crime4[data$republican == 1 & data$trump_trump == 1])
data$skipped21 <- ifelse(data$CC16_334d == "Skipped", 1, 0)

data$gay1 <- NA
data$gay1[data$CC16_335 == "Favor"] <- 1
data$gay1[data$CC16_335 == "Oppose"] <- 0
table(data$gay1)
a22 <- table(data$gay1[data$republican == 1 & data$never_trump_voter == 1])/length(data$gay1[data$republican == 1 & data$never_trump_voter == 1])
b22 <- table(data$gay1[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$gay1[data$republican == 1 & data$prim_no_trump_trump == 1])
c22 <- table(data$gay1[data$republican == 1 & data$trump_trump == 1])/length(data$gay1[data$republican == 1 & data$trump_trump == 1])
data$skipped22 <- ifelse(data$CC16_335 == "Skipped", 1, 0)

data$aca1 <- NA
data$aca1[data$CC16_351I == "Against"] <- 1
data$aca1[data$CC16_351I == "For"] <- 0
table(data$aca1)
a23 <- table(data$aca1[data$republican == 1 & data$never_trump_voter == 1])/length(data$aca1[data$republican == 1 & data$never_trump_voter == 1])
b23 <- table(data$aca1[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$aca1[data$republican == 1 & data$prim_no_trump_trump == 1])
c23 <- table(data$aca1[data$republican == 1 & data$trump_trump == 1])/length(data$aca1[data$republican == 1 & data$trump_trump == 1])
data$skipped23 <- ifelse(data$CC16_351I == "Skipped", 1, 0)

data$minwage1 <- NA
data$minwage1[data$CC16_351K == "For"] <- 1
data$minwage1[data$CC16_351K == "Against"] <- 0
table(data$minwage1)
a24 <- table(data$minwage1[data$republican == 1 & data$never_trump_voter == 1])/length(data$minwage1[data$republican == 1 & data$never_trump_voter == 1])
b24 <- table(data$minwage1[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$minwage1[data$republican == 1 & data$prim_no_trump_trump == 1])
c24 <- table(data$minwage1[data$republican == 1 & data$trump_trump == 1])/length(data$minwage1[data$republican == 1 & data$trump_trump == 1])
data$skipped24 <- ifelse(data$CC16_351K == "Skipped", 1, 0)

data$infra1 <- NA
data$infra1[data$CC16_351F == "For"] <- 1
data$infra1[data$CC16_351F == "Against"] <- 0
table(data$infra1)
a25 <- table(data$infra1[data$republican == 1 & data$never_trump_voter == 1])/length(data$infra1[data$republican == 1 & data$never_trump_voter == 1])
b25 <- table(data$infra1[data$republican == 1 & data$prim_no_trump_trump == 1])/length(data$infra1[data$republican == 1 & data$prim_no_trump_trump == 1])
c25 <- table(data$infra1[data$republican == 1 & data$trump_trump == 1])/length(data$infra1[data$republican == 1 & data$trump_trump == 1])
data$skipped25 <- ifelse(data$CC16_351F == "Skipped", 1, 0)

data$pct_liberal <- (data$gun1 + data$gun2 + data$gun3 + data$gun4 + data$immig1 + data$immig2 + data$immig3
                    + data$immig4 + data$abort1 + data$abort2 + data$abort3 + data$abort4 + data$abort5
                    + data$enviro1 + data$enviro2 + data$enviro3 + data$enviro4 + data$crime1 + data$crime2
                    + data$crime3 + data$crime4 + data$gay1 + data$aca1 + data$minwage1 + data$infra1)/25
data$pct_conservative <- 1 - data$pct_liberal

d.never.trump <- density(data$pct_conservative[data$never_trump_voter == 1 & data$republican == 1]
                          , na.rm = T)
d.gen.trump <- density(data$pct_conservative[data$prim_no_trump_trump == 1 & data$republican == 1]
                         , na.rm = T)
d.prim.trump <- density(data$pct_conservative[data$trump_trump == 1 & data$republican == 1]
                         , na.rm = T)
d.never.trump2 <- density(data$pct_conservative[data$never_trump == 1 & data$republican == 1]
                         , na.rm = T)

plot(d.never.trump, ylim = c(0, 3), col = "#ff0000", lwd = 2, xlab = "Percent Conservative", main = "", axes = F)
polygon(d.never.trump, col="#ff000010", border = NA)
text(.3, 1.5, "Never Trump", cex = .7, pos = 2)
lines(d.gen.trump, col = "#cc5f5f", lty = 2, lwd = 2)
polygon(d.gen.trump, col="#cc5f5f10", border = NA)
text(.82, 2.5, "General Election\nOnly", cex = .7, pos = 4)
lines(d.prim.trump, col = "#771d1d", lty = 3, lwd = 2)
polygon(d.prim.trump, col = "#771d1d10", border = NA)
text(.55, 2.8, "Primary & General\nElection", cex = .7, pos = 1)
segments(x0 = .55, x1 = .6, y0 = 2.45, y1 = 2, lwd = 2)
axis(1, at = seq(0, 1, .2))
axis(2, at = seq(0, 3, .5), las = 2)


################################################################
#Figure5 - ISSUE BY ISSUE
nt <- c(a1[1], a2[1], a3[1], a4[1], a5[1], a6[1], a7[1], a8[1], a9[1], a10[1], a11[1], 
        a12[1], a13[1], a14[1], a15[1], a16[1], a17[1], a18[1], 
        a19[1], a20[1], a21[1], a22[1], a23[1], a24[1], a25[1])

gt <- c(b1[1], b2[1], b3[1], b4[1], b5[1], b6[1], b7[1], b8[1], b9[1], b10[1], b11[1], 
        b12[1], b13[1], b14[1], b15[1], b16[1], b17[1], b18[1], 
        b19[1], b20[1], b21[1], b22[1], b23[1], b24[1], b25[1])

pt <- c(c1[1], c2[1], c3[1], c4[1], c5[1], c6[1], c7[1], c8[1], c9[1], c10[1], c11[1], 
        c12[1], c13[1], c14[1], c15[1], c16[1], c17[1], c18[1], 
        c19[1], c20[1], c21[1], c22[1], c23[1], c24[1], c25[1])

questions <- c("Background Checks\nFor Guns", "Prohibit List of\nGun Owners", 
                "Ban Assault\nRifles", "Increase Conceal\nCarry", 
                "Grant Legal Status\nto Immigrants", "Increase Border\nPatrol", 
                "DREAM\nAct", "Deport All\nIllegal Immigrants", "Always Allow\nAbortion", 
                "Prohibit Abortion\nafter 20 Weeks", 
                "Insurance Can\nDecline Abortions", 
                "No Federal\nAbortion Funds", "Make All\nAbortion Illegal", 
                "EPA Regulate\nCarbon", "Increase Fuel\nEfficiency", 
                "Require Alternative\nFuel", "Strengthen Environment\nRegulations", 
                "Eliminate Mandatory\nMinimum Sentences", "Require Policy\nBody Cameras", 
                "Increase Police\nOfficers", "Harsher Sentencing\nRepeat Offenders", "Allow Gay\nMarriage", 
                "Repeal\nACA", "Increase Minimum\nWage", "Infrastructure Spending")

nt2 <- nt[order(nt)]
pt2 <- pt[order(nt)]
gt2 <- gt[order(nt)]

questions2 <- questions[order(nt)]

par(mar = c(5, 7, 2, 2))
plot(0, 0, ylim = c(1, 25), xlim = c(0, 1), cex = 0, axes = F, xlab ="Proportion in Favor of Conservative Position on Policy", ylab = "")
abline(h = 1:25, lty = 2, col = "light grey")
points(x = gt2, y = 1:25, pch = 16, col = "#cc5f5f", cex = 1.5)
points(x = nt2, y = 1:25, pch = 15, col = "#ff0000", cex = 1.5)
points(x = pt2, y = 1:25, pch = 4, col = "#771d1d", cex = 1.5)
axis(1, at = seq(0, 1, .2))
axis(2, at = 1:25, labels = questions2, las = 2, cex.axis = .6)
box()
text(.1, 24.5, "Never Trump", pos = 4, cex = .5)
text(.1, 23.5, "General Election Only", pos = 4, cex = .5)
text(.1, 22.5, "Primary and General Election", pos = 4, cex = .5)
points(x = .09, y = 23.5, pch = 16, col = "#cc5f5f", cex = 1.5)
points(x = .09, y = 24.5, pch = 15, col = "#ff0000", cex = 1.5)
points(x = .09, y = 22.5, pch = 4, col = "#771d1d", cex = 1.5)


#########################################################
#Figure 6
cons <- read.csv("LIWC_open_ended.csv")

##NUMBER OF STATEMENTS COMBINED##
summary(cons$number.statements[cons$never_trump == 1])
summary(cons$number.statements[cons$general_trump == 1])
summary(cons$number.statements[cons$trump_trump == 1])

a <- table(cons$number.statements[cons$never_trump == 1])/length(cons$number.statements[cons$never_trump == 1])
b <- table(cons$number.statements[cons$general_trump == 1])/length(cons$number.statements[cons$general_trump == 1])
c <- table(cons$number.statements[cons$trump_trump == 1])/length(cons$number.statements[cons$trump_trump == 1])

par(mar = c(5, 5, 2, 2))
plot(0:7, a, xlim = c(-.2, 7.2), ylim = c(0, .35), cex = 0, ylab = "Proportion", xlab = "Number of Ideological Statements", axes = F)
segments(x0 = -.2:5.8, x1 = -.2:5.8, y0 = 0, y1 = a, lwd = 8, lend = 1, col = "#ff0000")
segments(x0 = 0:6, x1 = 0:6, y0 = 0, y1 = b, lwd = 8, lend = 1, col = "#cc5f5f")
segments(x0 = .2:8.2, x1 = .2:8.2, y0 = 0, y1 = c, lwd = 8, lend = 1, col = "#771d1d")
text(2.5, .21, "Never Trump (mean = 2.1) ", pos = 4, cex = .7)
text( 2.9, .19, "General Election Only (mean = 2.2)", pos = 4, cex = .7)
text(3.3, .17, "Primary & General Election (mean = 1.6)", pos = 4, cex = .7)
segments(x0 = 2.8, x1 = 2.75, y0 = .142, y1 = .2, lwd = 2)
segments(x0 = 3, x1 = 3.05, y0 = .17, y1 = .182, lwd = 2)
segments(x0 = 3.2, x1 = 3.5, y0 = .142, y1 = .165, lwd = 2)
axis(2, at = seq(0, 1, .05), las = 2)
axis(1, at = seq(0, 7, 1), lwd.ticks = 0)
box()
 
###############################################################
#Figure 7a
cons2 <- read.csv("LIWC_conservative.csv")

###################
#Trump-Trump voters
###################
size.of.government.trump <- table(cons2$sizeofgovernment[cons2$republican == 1 & cons2$trump_trump == 1])
size.of.government.trump <- size.of.government.trump/sum(size.of.government.trump)

constitution.trump <- table(cons2$constitution[cons2$republican == 1 & cons2$trump_trump == 1])
constitution.trump <- constitution.trump/sum(constitution.trump)

military.defense.trump <- table(cons2$militarydefense[cons2$republican == 1 & cons2$trump_trump == 1])
military.defense.trump <- military.defense.trump/sum(military.defense.trump)

religious.liberty.trump <- table(cons2$religiongodreligiousliberty[cons2$republican == 1 & cons2$trump_trump == 1])
religious.liberty.trump <- religious.liberty.trump/sum(religious.liberty.trump)

values.tradition.trump <- table(cons2$valuestradition[cons2$republican == 1 & cons2$trump_trump == 1])
values.tradition.trump <- values.tradition.trump/sum(values.tradition.trump)

abortion.trump <- table(cons2$abortion[cons2$republican == 1 & cons2$trump_trump == 1])
abortion.trump <- abortion.trump/sum(abortion.trump)

immigration.trump <- table(cons2$immigration[cons2$republican == 1 & cons2$trump_trump == 1])
immigration.trump <- immigration.trump/sum(immigration.trump)

individualism.trump <- table(cons2$individualism[cons2$republican == 1 & cons2$trump_trump == 1])
individualism.trump <- individualism.trump/sum(individualism.trump)

fiscal.trump <- table(cons2$fiscal[cons2$republican == 1 & cons2$trump_trump == 1])
fiscal.trump <- fiscal.trump/sum(fiscal.trump)

freedomlibertygeneral.trump <- table(cons2$freedomlibertygeneral[cons2$republican == 1 & cons2$trump_trump == 1])
freedomlibertygeneral.trump <- freedomlibertygeneral.trump/sum(freedomlibertygeneral.trump)

negative.trump <- table(cons2$negative[cons2$republican == 1 & cons2$trump_trump == 1])
negative.trump <- negative.trump/sum(negative.trump)

federalism.trump <- table(cons2$federalismstates[cons2$republican == 1 & cons2$trump_trump == 1])
federalism.trump <- federalism.trump/sum(federalism.trump)

guns.trump <- table(cons2$guns[cons2$republican == 1 & cons2$trump_trump == 1])
guns.trump <- guns.trump/sum(guns.trump)

market.regulation.trump <- table(cons2$marketregulation[cons2$republican == 1 & cons2$trump_trump == 1])
market.regulation.trump <- market.regulation.trump/sum(market.regulation.trump)

incoherent.trump <- table(cons2$incoherent[cons2$republican == 1 & cons2$trump_trump == 1])
incoherent.trump <- incoherent.trump/sum(incoherent.trump)

###################
#Not in Primary - General Trump voters
###################

size.of.government.g.trump <- table(cons2$sizeofgovernment[cons2$republican == 1 & cons2$general_trump == 1])
size.of.government.g.trump <- size.of.government.g.trump/sum(size.of.government.g.trump)

constitution.g.trump <- table(cons2$constitution[cons2$republican == 1 & cons2$general_trump == 1])
constitution.g.trump <- constitution.g.trump/sum(constitution.g.trump)

military.defense.g.trump <- table(cons2$militarydefense[cons2$republican == 1 & cons2$general_trump == 1])
military.defense.g.trump <- military.defense.g.trump/sum(military.defense.g.trump)

religious.liberty.g.trump <- table(cons2$religiongodreligiousliberty[cons2$republican == 1 & cons2$general_trump == 1])
religious.liberty.g.trump <- religious.liberty.g.trump/sum(religious.liberty.g.trump)

values.tradition.g.trump <- table(cons2$valuestradition[cons2$republican == 1 & cons2$general_trump == 1])
values.tradition.g.trump <- values.tradition.g.trump/sum(values.tradition.g.trump)

abortion.g.trump <- table(cons2$abortion[cons2$republican == 1 & cons2$general_trump == 1])
abortion.g.trump <- abortion.g.trump/sum(abortion.g.trump)

immigration.g.trump <- table(cons2$immigration[cons2$republican == 1 & cons2$general_trump == 1])
immigration.g.trump <- immigration.g.trump/sum(immigration.g.trump)

individualism.g.trump <- table(cons2$individualism[cons2$republican == 1 & cons2$general_trump == 1])
individualism.g.trump <- individualism.g.trump/sum(individualism.g.trump)

fiscal.g.trump <- table(cons2$fiscal[cons2$republican == 1 & cons2$general_trump == 1])
fiscal.g.trump <- fiscal.g.trump/sum(fiscal.g.trump)

freedomlibertygeneral.g.trump <- table(cons2$freedomlibertygeneral[cons2$republican == 1 & cons2$general_trump == 1])
freedomlibertygeneral.g.trump <- freedomlibertygeneral.g.trump/sum(freedomlibertygeneral.g.trump)

negative.g.trump <- table(cons2$negative[cons2$republican == 1 & cons2$general_trump == 1])
negative.g.trump <- negative.g.trump/sum(negative.g.trump)

federalism.g.trump <- table(cons2$federalismstates[cons2$republican == 1 & cons2$general_trump == 1])
federalism.g.trump <- federalism.g.trump/sum(federalism.g.trump)

guns.g.trump <- table(cons2$guns[cons2$republican == 1 & cons2$general_trump == 1])
guns.g.trump <- guns.g.trump/sum(guns.g.trump)

market.regulation.g.trump <- table(cons2$marketregulation[cons2$republican == 1 & cons2$general_trump == 1])
market.regulation.g.trump <- market.regulation.g.trump/sum(market.regulation.g.trump)

incoherent.g.trump <- table(cons2$incoherent[cons2$republican == 1 & cons2$general_trump == 1])
incoherent.g.trump <- incoherent.g.trump/sum(incoherent.g.trump)

###################
#Never Trumpers
###################

size.of.government.n.trump <- table(cons2$sizeofgovernment[cons2$republican == 1 & cons2$never_trump == 1])
size.of.government.n.trump <- size.of.government.n.trump/sum(size.of.government.n.trump)

constitution.n.trump <- table(cons2$constitution[cons2$republican == 1 & cons2$never_trump == 1])
constitution.n.trump <- constitution.n.trump/sum(constitution.n.trump)

military.defense.n.trump <- table(cons2$militarydefense[cons2$republican == 1 & cons2$never_trump == 1])
military.defense.n.trump <- military.defense.n.trump/sum(military.defense.n.trump)

religious.liberty.n.trump <- table(cons2$religiongodreligiousliberty[cons2$republican == 1 & cons2$never_trump == 1])
religious.liberty.n.trump <- religious.liberty.n.trump/sum(religious.liberty.n.trump)

values.tradition.n.trump <- table(cons2$valuestradition[cons2$republican == 1 & cons2$never_trump == 1])
values.tradition.n.trump <- values.tradition.n.trump/sum(values.tradition.n.trump)

abortion.n.trump <- table(cons2$abortion[cons2$republican == 1 & cons2$never_trump == 1])
abortion.n.trump <- abortion.n.trump/sum(abortion.n.trump)

immigration.n.trump <- table(cons2$immigration[cons2$republican == 1 & cons2$never_trump == 1])
immigration.n.trump <- immigration.n.trump/sum(immigration.n.trump)

individualism.n.trump <- table(cons2$individualism[cons2$republican == 1 & cons2$never_trump == 1])
individualism.n.trump <- individualism.n.trump/sum(individualism.n.trump)

fiscal.n.trump <- table(cons2$fiscal[cons2$republican == 1 & cons2$never_trump == 1])
fiscal.n.trump <- fiscal.n.trump/sum(fiscal.n.trump)

freedomlibertygeneral.n.trump <- table(cons2$freedomlibertygeneral[cons2$republican == 1 & cons2$never_trump == 1])
freedomlibertygeneral.n.trump <- freedomlibertygeneral.n.trump/sum(freedomlibertygeneral.n.trump)

negative.n.trump <- table(cons2$negative[cons2$republican == 1 & cons2$never_trump == 1])
negative.n.trump <- negative.n.trump/sum(negative.n.trump)

federalism.n.trump <- table(cons2$federalismstates[cons2$republican == 1 & cons2$never_trump == 1])
federalism.n.trump <- federalism.n.trump/sum(federalism.n.trump)

guns.n.trump <- table(cons2$guns[cons2$republican == 1 & cons2$never_trump == 1])
guns.n.trump <- guns.n.trump/sum(guns.n.trump)

market.regulation.n.trump <- table(cons2$marketregulation[cons2$republican == 1 & cons2$never_trump == 1])
market.regulation.n.trump <- market.regulation.n.trump/sum(market.regulation.n.trump)

incoherent.n.trump <- table(cons2$incoherent[cons2$republican == 1 & cons2$never_trump == 1])
incoherent.n.trump <- incoherent.n.trump/sum(incoherent.n.trump)

############################################

nt <- c(size.of.government.n.trump[2], constitution.n.trump[2], military.defense.n.trump[2], religious.liberty.n.trump[2], 
        values.tradition.n.trump[2], abortion.n.trump[2], immigration.n.trump[2], individualism.n.trump[2], 
        fiscal.n.trump[2], freedomlibertygeneral.n.trump[2], negative.n.trump[2], 
        federalism.n.trump[2], guns.n.trump[2], market.regulation.n.trump[2], incoherent.n.trump[2])
nt[is.na(nt)] <- 0.0001

gt <- c(size.of.government.g.trump[2], constitution.g.trump[2], military.defense.g.trump[2], religious.liberty.g.trump[2],
        values.tradition.g.trump[2], abortion.g.trump[2], immigration.g.trump[2], individualism.g.trump[2], 
        fiscal.g.trump[2], freedomlibertygeneral.g.trump[2], negative.g.trump[2], 
        federalism.g.trump[2], guns.g.trump[2], market.regulation.g.trump[2], incoherent.g.trump[2])
gt[is.na(gt)] <- 0.0001

pt <- c(size.of.government.trump[2], constitution.trump[2], military.defense.trump[2],  religious.liberty.trump[2], 
        values.tradition.trump[2], abortion.trump[2], immigration.trump[2], individualism.trump[2], fiscal.trump[2],
        freedomlibertygeneral.trump[2], negative.trump[2], federalism.trump[2],
        guns.trump[2], market.regulation.trump[2], incoherent.trump[2])
pt[is.na(pt)] <- 0.0001

order(nt)

pt.2 <- pt[c(15, 11, 3, 6, 7, 13, 4, 12, 14, 1, 10, 5, 9, 2, 8)]
nt.2 <- nt[c(15, 11, 3, 6, 7, 13, 4, 12, 14, 1, 10, 5, 9, 2, 8)]
gt.2 <- gt[c(15, 11, 3, 6, 7, 13, 4, 12, 14, 1, 10, 5, 9, 2, 8)]

questions <- c("Size of\nGovernment", "Constitution", "Military/\nDefense", "God/Religious\nLiberty" ,
               "Values/\nTradition", "Abortion", "Immigration", "Individualism", 
               "Fiscal\nIssues", "Freedom/\nLiberty", "Negative\nStatements",
               "Federalism", "2nd\nAmendment", "Regulation/\nFree Market", "Incoherent\nResponse") 

questions.2 <- questions[c(15, 11, 3, 6, 7, 13, 4, 12, 14, 1, 10, 5, 9, 2, 8)]

par(mar = c(5, 9, 2, 2))
plot(0, 0, ylim = c(.6, 15), xlim = c(0, 1), cex = 0, axes = F, ylab ="", 
     xlab = "Proportion Mentioning Topic")
segments(x0 = 0, x1 = nt.2, y0 = .8:15.8, y1 = .8:15.8, lend = 1, lwd = 8, col = "#ff0000")
segments(x0 = 0, x1 = gt.2, y0 = 1:15, y1 = 1:15, lend = 1, lwd = 8, col = "#cc5f5f")
segments(x0 = 0, x1 = pt.2, y0 = 1.2:15.2, y1 = 1.2:15.2, lend = 1, lwd = 8, col = "#771d1d")
text(.3, .8, "Never Trump", cex = .7, pos = 4)
text(.3, 1, "General Election Only", cex = .7, pos = 4)
text(.3, 1.2, "Primary & General Election", cex = .7, pos = 4)
abline(h=2.5, lty = 2, col = "grey")
axis(1, at = seq(0, 1, .2))
axis(2, at = seq(1, 15, 1), labels = questions.2, las = 2, cex = .8)


###############################################################
#Figure 7b
liberal <- read.csv("LIWC_liberal.csv")

###################
#Trump-Trump voters
###################
religion.trump <- table(liberal$religion.god[liberal$republican == 1 & liberal$trump_trump == 1])
religion.trump <- religion.trump/sum(religion.trump)

tax.trump <- table(liberal$tax[liberal$republican == 1 & liberal$trump_trump == 1])
tax.trump <- tax.trump/sum(tax.trump)

values.trump <- table(liberal$values.tradition[liberal$republican == 1 & liberal$trump_trump == 1])
values.trump <- values.trump/sum(values.trump)

constitution.trump <- table(liberal$constitution[liberal$republican == 1 & liberal$trump_trump == 1])
constitution.trump <- constitution.trump/sum(constitution.trump)

size.government.trump <- table(liberal$size.gov[liberal$republican == 1 & liberal$trump_trump == 1])
size.government.trump <- size.government.trump/sum(size.government.trump)

redistribution.trump <- table(liberal$redistribution[liberal$republican == 1 & liberal$trump_trump == 1])
redistribution.trump <- redistribution.trump/sum(redistribution.trump)

environment.trump <- table(liberal$environment[liberal$republican == 1 & liberal$trump_trump == 1])
environment.trump <- environment.trump/sum(environment.trump)

individualism.trump <- table(liberal$no.individualism[liberal$republican == 1 & liberal$trump_trump == 1])
individualism.trump <- individualism.trump/sum(individualism.trump)

poor.trump <- table(liberal$poor[liberal$republican == 1 & liberal$trump_trump == 1])
poor.trump <- poor.trump/sum(poor.trump)

minorities.trump <- table(liberal$minorities[liberal$republican == 1 & liberal$trump_trump == 1])
minorities.trump <- minorities.trump/sum(minorities.trump)

labels.trump <- table(liberal$labels[liberal$republican == 1 & liberal$trump_trump == 1])
labels.trump <- labels.trump/sum(labels.trump)

guns.trump <- table(liberal$guns[liberal$republican == 1 & liberal$trump_trump == 1])
guns.trump <- guns.trump/sum(guns.trump)

immigration.trump <- table(liberal$immigration[liberal$republican == 1 & liberal$trump_trump == 1])
immigration.trump <- immigration.trump/sum(immigration.trump)

political.correctness.trump <- table(liberal$political.correctness[liberal$republican == 1 & liberal$trump_trump == 1])
political.correctness.trump <- political.correctness.trump/sum(political.correctness.trump)

abortion.trump <- table(liberal$abortion[liberal$republican == 1 & liberal$trump_trump == 1])
abortion.trump <- abortion.trump/sum(abortion.trump)

incoherent.trump <- table(liberal$incoherent[liberal$republican == 1 & liberal$trump_trump == 1])
incoherent.trump <- incoherent.trump/sum(incoherent.trump)

insult.trump <- table(liberal$insult[liberal$republican == 1 & liberal$trump_trump == 1])
insult.trump <- insult.trump/sum(insult.trump)

###################
#Not in Primary - General Trump voters
###################

religion.g.trump <- table(liberal$religion.god[liberal$republican == 1 & liberal$general_trump == 1])
religion.g.trump <- religion.g.trump/sum(religion.g.trump)

tax.g.trump <- table(liberal$tax[liberal$republican == 1 & liberal$general_trump == 1])
tax.g.trump <- tax.g.trump/sum(tax.g.trump)

values.g.trump <- table(liberal$values.tradition[liberal$republican == 1 & liberal$general_trump == 1])
values.g.trump <- values.g.trump/sum(values.g.trump)

constitution.g.trump <- table(liberal$constitution[liberal$republican == 1 & liberal$general_trump == 1])
constitution.g.trump <- constitution.g.trump/sum(constitution.g.trump)

size.government.g.trump <- table(liberal$size.gov[liberal$republican == 1 & liberal$general_trump == 1])
size.government.g.trump <- size.government.g.trump/sum(size.government.g.trump)

redistribution.g.trump <- table(liberal$redistribution[liberal$republican == 1 & liberal$general_trump == 1])
redistribution.g.trump <- redistribution.g.trump/sum(redistribution.g.trump)

environment.g.trump <- table(liberal$environment[liberal$republican == 1 & liberal$general_trump == 1])
environment.g.trump <- environment.g.trump/sum(environment.g.trump)

individualism.g.trump <- table(liberal$no.individualism[liberal$republican == 1 & liberal$general_trump == 1])
individualism.g.trump <- individualism.g.trump/sum(individualism.g.trump)

poor.g.trump <- table(liberal$poor[liberal$republican == 1 & liberal$general_trump == 1])
poor.g.trump <- poor.g.trump/sum(poor.g.trump)

minorities.g.trump <- table(liberal$minorities[liberal$republican == 1 & liberal$general_trump == 1])
minorities.g.trump <- minorities.g.trump/sum(minorities.g.trump)

labels.g.trump <- table(liberal$labels[liberal$republican == 1 & liberal$general_trump == 1])
labels.g.trump <- labels.g.trump/sum(labels.g.trump)

guns.g.trump <- table(liberal$guns[liberal$republican == 1 & liberal$general_trump == 1])
guns.g.trump <- guns.g.trump/sum(guns.g.trump)

immigration.g.trump <- table(liberal$immigration[liberal$republican == 1 & liberal$general_trump == 1])
immigration.g.trump <- immigration.g.trump/sum(immigration.g.trump)

political.correctness.g.trump <- table(liberal$political.correctness[liberal$republican == 1 & liberal$general_trump == 1])
political.correctness.g.trump <- political.correctness.g.trump/sum(political.correctness.g.trump)

abortion.g.trump <- table(liberal$abortion[liberal$republican == 1 & liberal$general_trump == 1])
abortion.g.trump <- abortion.g.trump/sum(abortion.g.trump)

incoherent.g.trump <- table(liberal$incoherent[liberal$republican == 1 & liberal$general_trump == 1])
incoherent.g.trump <- incoherent.g.trump/sum(incoherent.g.trump)

insult.g.trump <- table(liberal$insult[liberal$republican == 1 & liberal$general_trump == 1])
insult.g.trump <- insult.g.trump/sum(insult.g.trump)

###################
#Never Trumpers
###################

religion.n.trump <- table(liberal$religion.god[liberal$republican == 1 & liberal$never_trump == 1])
religion.n.trump <- religion.n.trump/sum(religion.n.trump)

tax.n.trump <- table(liberal$tax[liberal$republican == 1 & liberal$never_trump == 1])
tax.n.trump <- tax.n.trump/sum(tax.n.trump)

values.n.trump <- table(liberal$values.tradition[liberal$republican == 1 & liberal$never_trump == 1])
values.n.trump <- values.n.trump/sum(values.n.trump)

constitution.n.trump <- table(liberal$constitution[liberal$republican == 1 & liberal$never_trump == 1])
constitution.n.trump <- constitution.n.trump/sum(constitution.n.trump)

size.government.n.trump <- table(liberal$size.gov[liberal$republican == 1 & liberal$never_trump == 1])
size.government.n.trump <- size.government.n.trump/sum(size.government.n.trump)

redistribution.n.trump <- table(liberal$redistribution[liberal$republican == 1 & liberal$never_trump == 1])
redistribution.n.trump <- redistribution.n.trump/sum(redistribution.n.trump)

environment.n.trump <- table(liberal$environment[liberal$republican == 1 & liberal$never_trump == 1])
environment.n.trump <- environment.n.trump/sum(environment.n.trump)

individualism.n.trump <- table(liberal$no.individualism[liberal$republican == 1 & liberal$never_trump == 1])
individualism.n.trump <- individualism.n.trump/sum(individualism.n.trump)

poor.n.trump <- table(liberal$poor[liberal$republican == 1 & liberal$never_trump == 1])
poor.n.trump <- poor.n.trump/sum(poor.n.trump)

minorities.n.trump <- table(liberal$minorities[liberal$republican == 1 & liberal$never_trump == 1])
minorities.n.trump <- minorities.n.trump/sum(minorities.n.trump)

labels.n.trump <- table(liberal$labels[liberal$republican == 1 & liberal$never_trump == 1])
labels.n.trump <- labels.n.trump/sum(labels.n.trump)

guns.n.trump <- table(liberal$guns[liberal$republican == 1 & liberal$never_trump == 1])
guns.n.trump <- guns.n.trump/sum(guns.n.trump)

immigration.n.trump <- table(liberal$immigration[liberal$republican == 1 & liberal$never_trump == 1])
immigration.n.trump <- immigration.n.trump/sum(immigration.n.trump)

political.correctness.n.trump <- table(liberal$political.correctness[liberal$republican == 1 & liberal$never_trump == 1])
political.correctness.n.trump <- political.correctness.n.trump/sum(political.correctness.n.trump)

abortion.n.trump <- table(liberal$abortion[liberal$republican == 1 & liberal$never_trump == 1])
abortion.n.trump <- abortion.n.trump/sum(abortion.n.trump)

insult.n.trump <- table(liberal$insult[liberal$republican == 1 & liberal$never_trump == 1])
insult.n.trump <- insult.n.trump/sum(insult.n.trump)

incoherent.n.trump <- table(liberal$incoherent[liberal$republican == 1 & liberal$never_trump == 1])
incoherent.n.trump <- incoherent.n.trump/sum(incoherent.n.trump)

############################################

nt <- c(religion.n.trump[2], tax.n.trump[2], values.n.trump[2], constitution.n.trump[2], size.government.n.trump[2], 
        redistribution.n.trump[2], individualism.n.trump[2], poor.n.trump[2], minorities.n.trump[2], 
        labels.n.trump[2], guns.n.trump[2], immigration.n.trump[2], political.correctness.n.trump[2], 
        insult.n.trump[2], incoherent.n.trump[2])
nt[is.na(nt)] <- 0.0001

gt <- c(religion.g.trump[2], tax.g.trump[2], values.g.trump[2], constitution.g.trump[2], size.government.g.trump[2], 
        redistribution.g.trump[2], individualism.g.trump[2], poor.g.trump[2], minorities.g.trump[2], 
        labels.g.trump[2], guns.g.trump[2], immigration.g.trump[2], political.correctness.g.trump[2], 
        insult.g.trump[2], incoherent.g.trump[2])
gt[is.na(gt)] <- 0.0001

pt <- c(religion.trump[2], tax.trump[2], values.trump[2], constitution.trump[2], size.government.trump[2], 
        redistribution.trump[2], individualism.trump[2], poor.trump[2], minorities.trump[2], 
        labels.trump[2], guns.trump[2], immigration.trump[2], political.correctness.trump[2], 
        insult.trump[2], incoherent.trump[2])
pt[is.na(pt)] <- 0.0001

order(nt)

pt.2 <- pt[c(15, 14, 1, 4, 11, 12, 13, 3, 8, 10, 9, 2, 7, 5, 6)]
nt.2 <- nt[c(15, 14, 1, 4, 11, 12, 13, 3, 8, 10, 9, 2, 7, 5, 6)]
gt.2 <- gt[c(15, 14, 1, 4, 11, 12, 13, 3, 8, 10, 9, 2, 7, 5, 6)]

questions <- c("God/Religious\nLiberty", "Fiscal\nIssues", "Values\nTradition", "Constitution",
               "Size of\nGovernment", "Redistribution", "Individualism", "The Poor",
               "Minority\nGroups", "Political Labels\nSocialist/Marxist", "2nd\nAmendment",
               "Immigration", "Political\nCorrectness", "Ad Hominem", "Incoherent\nResponse") 

questions.2 <- questions[c(15, 14, 1, 4, 11, 12, 13, 3, 8, 10, 9, 2, 7, 5, 6)]

par(mar = c(5, 9, 2, 2))
plot(0, 0, ylim = c(.6, 15), xlim = c(0, 1), cex = 0, axes = F, ylab ="", 
     xlab = "Proportion Mentioning Topic")
segments(x0 = 0, x1 = nt.2, y0 = .8:15.8, y1 = .8:15.8, lend = 1, lwd = 8, col = "#ff0000")
segments(x0 = 0, x1 = gt.2, y0 = 1:15, y1 = 1:15, lend = 1, lwd = 8, col = "#cc5f5f")
segments(x0 = 0, x1 = pt.2, y0 = 1.2:15.2, y1 = 1.2:15.2, lend = 1, lwd = 8, col = "#771d1d")
text(.4, .8, "Never Trump", cex = .7, pos = 4)
text(.4, 1, "General Election Only", cex = .7, pos = 4)
text(.4, 1.2, "Primary & General Election", cex = .7, pos = 4)
abline(h=2.5, lty = 2, col = "grey")
axis(1, at = seq(0, 1, .2))
axis(2, at = seq(1, 15, 1), labels = questions.2, las = 2, cex = .8)


##################################################
#Table 3 - REGRESSION ANALYSIS

#Model 1
summary(model1 <- lm(ideology ~ never_trump_voter + prim_no_trump_trump + age + income + 
                        male + white + unemploy + education, data = data[data$republican == 1 & (data$never_trump_voter == 1 | 
                       data$prim_no_trump_trump == 1 | data$trump_trump == 1),]))
length(model1$resid)

#Model 2
summary(model2 <- lm(pct_conservative ~ never_trump_voter + prim_no_trump_trump + age + income + 
                        male + white + unemploy + education, data = data[data$republican == 1 
                         & (data$never_trump_voter == 1 | data$prim_no_trump_trump == 1 | data$trump_trump == 1),]))
length(model2$resid)

#Model 3
summary(model3 <- glm(incoherent ~ never_trump + general_trump + age + income + 
                         male + white + unemployed + educ, data = cons, family = binomial(link = "probit")))
length(model3$resid)

#Model 4
summary(model4 <- lm(number.statements ~ never_trump + general_trump + age + income + 
                       male + white + unemployed + educ, data = cons))                                                                          
length(model4$resid)



